<?php
/**
 * @Author: 玛瑙
 * @Date  :   2018-04-20 14:12:42
 * @Last  Modified by:   Administrator
 * @Last  Modified time: 2018-04-23 10:27:20
 */
namespace app\company\controller;

class Company extends Common
{
    /**
     * 企业信息管理
     */
    public function index()
    {
        $map['id'] = session('company.company_id');
        $map['is_delete'] = 0;
        $company_info = get_info('company', $map, '');
        // 当前店铺的地址信息
        $company_info['info']['province'] = $company_info['province'];
        $company_info['info']['city'] = $company_info['city'];
        $company_info['info']['district'] = $company_info['district'];
        // 所有地址信息,下拉列表
        $company_info['province'] = db('region')->where('level', 1)->select();
        if ($company_info['info']['province']) {
            $company_info['city'] = db('region')->where('parent_id', $company_info['info']['province'])->select();
        }
        if ($company_info['info']['city']) {
            $company_info['district'] = db('region')->where('parent_id', $company_info['info']['city'])->select();
        }
        $vehicle_ids = explode(',', $company_info['vehicle_ids']);
        $company_info['vehicle'] = db('vehicle')->select();
        foreach ($company_info['vehicle'] as $key => $value) {
            foreach ($vehicle_ids as $v) {
                // 循环比对车型ID与检测站可检测车型, 若相同则追加checked = true
                if ($value['id'] == $v) {
                    $company_info['vehicle'][$key]['checked'] = true;
                }
            }
        }
        return view('', $company_info);
    }

    /**
     * 修改企业信息
     */
    public function doModify()
    {
        $data = input('post.');
        $data['vehicle_ids'] = implode(',', $data['vehicle_ids']);
        // 腾讯地址解析 start
        $province = get_value($data['province'], 'region', 'id', 'name', '1');
        $city = get_value($data['city'], 'region', 'id', 'name', '1');
        $district = get_value($data['district'], 'region', 'id', 'name', '1');
        $address = $province . $city . $district . $data['company_addr'];
        $url = config('tx_lbs.address'); // 腾讯地址解析
        $key = config('tx_lbs.key'); // 腾讯地址解析
        $url = sprintf($url, $address, $key);
        $result = https_request($url);
        $result = json_decode($result, true);
        if (0 == $result['status']) {
            $data['longitude'] = $result['result']['location']['lng'];
            $data['latitude'] = $result['result']['location']['lat'];
        }
        // 腾讯地址解析 end
        if (!checkTelephone($data['telephone'])) {
            return json([
                'statusCode' => 300,
                'message'    => '企业电话格式错误',
            ]);
        }
        if (!checkMobile($data['shop_leader_phone'])) {
            return json([
                'statusCode' => 300,
                'message'    => '负责人电话格式错误',
            ]);
        }
        try {
            doModify('company', $data);
        } catch (Exception $e) {
            return json([
                'statusCode' => 300,
                'message'    => $e->getMessage(),
            ]);
        }
        return json([
            'statusCode' => 200,
            'message'    => '修改成功',
            'navTabId'   => 'company_manage',
        ]);
    }

    /**
     * 省市区选择
     */
    public function selectajax()
    {
        $category_one = input('get.id');
        $data = [
            'parent_id' => $category_one,
        ];
        $list = db("region")->field('id,name')->where($data)->select();
        $text[] = ['', '--全部--'];
        foreach ($list as $v) {
            $text[] = [$v['id'], $v['name']];
        }
        echo json_encode($text);
    }

    public function xml_to_json($source)
    {
        if (is_file($source)) { //传的是文件，还是xml的string的判断
            $xml_array = simplexml_load_file($source);
        } else {
            $xml_array = simplexml_load_string($source);
        }
        $json = json_encode($xml_array); //php5，以及以上，如果是更早版本，请查看JSON.php
        return $json;
    }
}